﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using DBEngine.Elements;
using System.Data;
using System.Data.SqlClient;

namespace DBEngine.Tables
{
    class LensesExaminationTable : ExaminationTable
    {
        protected static Dictionary<Examination.Fields, SqlDbType> TABLE_FIELDS;

         static LensesExaminationTable()
        {
            TABLE_FIELDS = new Dictionary<Examination.Fields, SqlDbType>();
            TABLE_FIELDS.Add(Examination.Fields.BC, SqlDbType.Float);
            TABLE_FIELDS.Add(Examination.Fields.DIA, SqlDbType.Float);
            TABLE_FIELDS.Add(Examination.Fields.KR, SqlDbType.Float);
        }

        internal LensesExaminationTable()
            : base("LensesExamination", TABLE_FIELDS){ }

        override protected Object ReadRow(SqlDataReader reader)
        {
            LensesExamination retval = new LensesExamination();
            Examination.Fields colName;
            foreach (KeyValuePair<Examination.Fields, SqlDbType> pair in mTableFields)
            {
                colName = pair.Key;

                if (colName == Examination.Fields.ID || colName == Examination.Fields.ClientID || colName == Examination.Fields.Date)
                    retval[colName] = reader[colName.ToString()];
                else
                {
                    ExaminationElement element = new ExaminationElement();
                    foreach (ExaminationElement.Fields field in Enum.GetValues(typeof(ExaminationElement.Fields)))
                    {

                        element[field] = Convert.ToDouble(reader[colName.ToString() + "_" + field.ToString()]);
                    }
                    retval[colName] = element;
                }
            }

            return retval;
        }
       
    }
}
